home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / unix.doc < prev    next >
Text File  |  1996-12-01  |  16KB  |  404 lines

  1. Unix compatibility library for SAS/C 6.55+
  2. ------------------------------------------
  3.  
  4. Note: this is an edited version of the original doc written by David Gay
  5. to reflect the changes I introduced.
  6.  
  7. Introduction
  8. ------------
  9.  
  10. This library provides 110+ odd functions which are useful for porting
  11. Unix programs to the Amiga running AmigaDOS 2.04 or higher. It is thus
  12. similar to ixemul.library, but has more restricted aims:
  13.  
  14. a) It is written specifically for SAS/C 6.55+.
  15.  
  16. b) It isn't a complete C library, it requires the SAS/C library to function.
  17.    It adds some routines, and replaces others that were deficient, out of date,
  18.    or that didn't provide adequate functionality.
  19.  
  20. c) It isn't a complete Unix emulation library, it only contains those functions
  21.    that I needed while porting various utilities (mainly from GNU) to the Amiga.
  22.  
  23. d) It is a traditional C link library, not an Amiga library. This produces 
  24.    bigger executables.
  25.  
  26. Considering the above points, you might ask why David wrote it ...
  27. There are several good answers:
  28.  
  29. a) He started it before he was aware of ixemul.library.
  30.  
  31. b) ixemul.library didn't provide the features he needed for Emacs (support for
  32.    select).
  33.  
  34. c) It was easier for him to maintain when he found the need to support another
  35.    Unix feature (eg deleting open files).
  36.  
  37. As regard me, I updated it to SAS/C 6.55+ and added other improvements to
  38. be able to port X11 programs to AmiWin.
  39.  
  40. Copying
  41. -------
  42.  
  43. The library includes code that David (and I) have written, which he (and I)
  44. place in the public domain. This is found in all the files that don't have a
  45. Copyright notice.
  46.  
  47. It also includes code which is (inclusive of my modifications)
  48.  
  49.  Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
  50.  All rights reserved.
  51.  
  52. This code is freely redistributable (see the copyright notices in the source
  53. and include files).
  54.  
  55. Finally it includes Stefan Proels's public domain alloca implementation
  56. specifically written for SAS/C 6.55+.
  57.  
  58. Installation & Use
  59. ------------------
  60.  
  61. To use this library, you must compile with the include directory in your
  62. include search path, and link with the unix.lib library (which must be
  63. specified before sc.lib). For example, if you extract this archive in a
  64. directory called src: and assign uinclude: to src:unix/include and
  65. ulib: to src:unix, you could compile the following program:
  66.  
  67. echo.c:
  68.  
  69. #include <string.h>
  70. #include <unistd.h>
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.   int i;
  75.  
  76.   for (i = 1; i < argc; i++) {
  77.       if (i != 1) write(1, " ", 1);
  78.       write(1, argv[i], strlen(argv[i]));
  79.   }
  80.   write(1, "\n", 1);
  81.   return 0;
  82. }
  83.  
  84. with the command
  85.  
  86.   sc link idir=uinclude: lib=ulib:unix.lib echo.c
  87.  
  88. to produce a simple unix-like echo command.
  89.  
  90. Among other improvements, I added support for sockets code, relying for this
  91. on AmiTCP's bsdsocket.library. If you need compiling code containing socket
  92. calls, than you should have AmiTCP, its include files in netinclude: and
  93.  
  94. 1) compile with AMITCP defined (DEF=AMITCP)
  95. 2) add netinclude: AFTER uinclude: in the include search path
  96. 3) have an #include <proto/socket.h> in your code
  97.  
  98. So the command to compile prog.c (containing socket calls) should be like
  99.  
  100.   sc link DEF=AMITCP idir=uinclude: idir=netinclude: lib=ulib:unix.lib prog.c
  101.  
  102. Of course, for compiling code containing socket calls, you could instead use
  103. net.lib, the link library distributed with AmiTCP, but it lacks support for
  104. pipes, i.e. the pipe() call. This unix.lib does not have the complete
  105. functionality of net.lib as regard socket code support but I think it is
  106. sufficient for compiling the vast majority of programs using network sockets.
  107.  
  108. You should define the following environment variables:
  109.  
  110. USER     - A user name for the sole Amiga user (default "user").
  111. USERNAME - The full name of the sole Amiga user (default $USER).
  112. HOME     - A "home" directory (default "s:") for programs that want one.
  113.        Configuration files will probably end up here ...
  114. SHELL    - A program which behaves reasonably like a Unix shell
  115.        (default "bin:sh"). You should copy the sh executable there if
  116.        you don't have a Unix-like shell.
  117. HOSTNAME - The name of your machine (default "amiga").
  118.  
  119. If you are going to be using pipes, you will require Matt Dillon's fifo.library
  120. and fifo: device. This can be found on the Fish disks, with his UUCP
  121. distribution and on any Aminet site.
  122.  
  123. If you use the popen() and pclose() calls you also need the APipe-Handler.
  124. Again you can find it on any Aminet site.
  125.  
  126. If you want to install the timezone information (see the discussion below on
  127. Unix vs Amiga time), do the following:
  128.  
  129.   a) change to the zoneinfo directory
  130.   b) compile the zic program by running smake.
  131.   c) change to the datfiles directory.
  132.   d) edit smakefile and choose your timezone (you can look at the data files
  133.      to see the ones available). If you get this wrong, you can always change
  134.      it later with the zic program.
  135.   e) type 'smake install'. This will compile the timezones, and setup the one
  136.      you chose as the default. The default can be changed with
  137.        zic -l <timezone name>
  138.  
  139.      or by defining the environment variable TZ.
  140.  
  141. Functionality
  142. -------------
  143.  
  144. While this library aims to hide the differences between AmigaDOS & Unix, 
  145. it also aims to provide support for Amiga specific features. This sometimes
  146. produces strange compromises.
  147.  
  148. This library provides two things to programs linked with it:
  149.  
  150. a) A Unix-like environment:
  151.  
  152.    This includes:
  153.  
  154.    - Unix-like command line parsing, with wildcard expansion. These
  155.      wildcards are however specified with the Amiga syntax.
  156.  
  157.    Arguments which are unquoted or surround with single quotes are
  158.    handled like Unix shells. Those surrounded with double quotes are
  159.    handled in the Amiga fashion, but with wildcard expansion (this is
  160.    done to avoid problems with the way the exec function works).
  161.  
  162.    Here is a summary of argument splitting:
  163.  
  164.    Arguments can be enclosed in single quotes, (') double quotes ("), or
  165.    separated by spaces.
  166.  
  167.    Arguments enclosed by single quotes never suffer wildcard expansion, and
  168.    no character is significant inside them (not even \). Given the echo program
  169.    given above,
  170.  
  171.     echo 'f*un\'
  172.  
  173.    simply displays
  174.  
  175.     f*un\
  176.  
  177.    Within double quotes, * is the standard Amiga escape character. \ is
  178.    handled just like any other character. Wildcards are expanded. So,
  179.    assuming the current directory contains only echo.c and echo,
  180.  
  181.     echo "#?.c" "*.c" "\mad"
  182.  
  183.    displays
  184.  
  185.     echo.c .c \mad
  186.  
  187.    (* escapes the following character, except that *N is newline and *E is
  188.    escape. Wildcard characters still behave as usual (' is the standard
  189.    Amiga wildcard escape character)).
  190.  
  191.    If an argument is unquoted, \ acts as an escape character (removing
  192.    special significance from the next character, be it a wildcard, a space,
  193.    a \, ...). So
  194.  
  195.     echo \*.c \\ \n
  196.  
  197.    displays
  198.  
  199.     *.c \ n
  200.  
  201.    - When a program is run from the Workbench, stdin, stdout & stderr are
  202.      opened on NIL:, and the icons selected are converted to file names and
  203.      passed as the argc,argv to main.
  204.  
  205.    - The standard variable environ is defined and contains all the local
  206.      environment variables. This is passed as the envp parameter to main.
  207.  
  208.    - The program is led to believe that all files belong to $USER (uid 1),
  209.      group wheel (gid 0).
  210.  
  211.    - Amiga protection flags are mapped onto the standard 12 Unix protection
  212.      bits (and back when necessary). This can be overridden (dynamically) by
  213.      changing the value of use_amiga_flags. Eg:
  214.  
  215.     extern int use_amiga_flags;
  216.  
  217.     ...
  218.  
  219.  
  220.     main()
  221.     {
  222.       use_amiga_flags = 1;
  223.  
  224.       ... some code using stat or chmod or ...
  225.     }
  226.  
  227.      In this case, the Amiga protection bits are left untouched. Otherwise the
  228.      mapping is as follows (note that the archive bit is lost):
  229.  
  230.      Unix -> Amiga
  231.  
  232.      Amiga read: if user, group or world read.
  233.      Amiga write: if user or group write.
  234.      Amiga delete: if user or world write.
  235.      Amiga execute: if group execute or only user execute.
  236.      Amiga script: if world execute or only user execute.
  237.      Amiga pure: if sticky.
  238.  
  239.      Amiga -> Unix
  240.  
  241.      user, group, world read: if amiga read.
  242.      user write: if amiga write and delete.
  243.      group write: if amiga write.
  244.      world write: if amiga delete.
  245.      user execute: if amiga execute or amiga script.
  246.      group execute: if amiga execute.
  247.      world execute: if amiga script.
  248.      sticky: if amiga pure.
  249.  
  250.    - Unix-like time. time is expressed in seconds since 1-Jan-1970 00:00 GMT.
  251.      This is the format used by the time(), stat() and utime() functions. The
  252.      library uses the BSD time functions which properly handle timezones,
  253.      daylight savings time, etc.
  254.  
  255.      On Unix systems, time is generally stored as GMT, while the Amiga stores
  256.      local time. These times are visible in two places: the time stored in the
  257.      system clock and the creation, modification or access dates for files
  258.      (only the modification date is available on the Amiga). There are 3 ways
  259.      to resolve this conflict:
  260.  
  261.      1) Ignore timezones (you can choose this option by not installing the
  262.     timezone information). The library will then assume that system time
  263.     and file times are expressed in GMT and will never apply any
  264.     corrections. All will work well (except maybe on a global network).
  265.     This is the simplest solution.
  266.  
  267.      2) System & file time are assumed to be local times. The time() & stat()
  268.     functions convert local time to GMT, the utime() call converts GMT to
  269.     local time. This allows you to keep the system clock on local time,
  270.     which is compatible with most (all?) Amiga applications. However,
  271.     nearly every program will be bigger because they will need the timezone
  272.     information and timezone conversion code (approximately 7k). The
  273.     library can be recompiled if you prefer to have things this way by
  274.     adding `DEF USE_LOCAL=1' to the DEFS variable in smakefile.
  275.  
  276.      3) System & file time are stored in GMT. Times are converted to local time
  277.     only when they are displayed to the user, who must of course choose the
  278.     correct timezone. This entails changing your system clock to GMT, which
  279.     means that most programs will display an incorrect time. However, you
  280.     won't have to change your system clock when daylight savings time ends.
  281.     The library comes compiled this way (But you can still choose option 1,
  282.     no timezone information, and avoid the potential confusion).
  283.  
  284.    - BSD-like signals. Signal handlers stay enabled after a signal occurs
  285.      (that particular signal is simply masked for the duration of the handler).
  286.      setjmp & longjmp preserve the signal mask (the functions _setjmp & _longjmp
  287.      don't). You may receive any signal with kill(getpid(), sig). Otherwise only
  288.      the following occur:
  289.  
  290.     SIGINT:  user typed ctrl-c
  291.     SIGQUIT: user typed ctrl-d
  292.     SIGALRM: alarm() expired
  293.     SIGCHLD: a child process died
  294.     SIGIO:   asynhronous I/O is pending   | these can be triggered only by
  295.     SIGURG:  urgent data is pending       | bsdsocket.library
  296.  
  297.      When using kill to signal another process, only SIGHUP, SIGINT, SIGQUIT &
  298.      SIGKILL do anything. They all send SIGBREAKF_CTRL_C | D to the process (and
  299.      hopefully its children). See the discussion under the exec function.
  300.  
  301.  
  302. b) A large number of unix C library calls:
  303.  
  304.  
  305.    _exit, _setjmp, _longjmp, abort, alarm, access, alloca, bcmp, bcopy, bzero,
  306.    chmod, chown, fchown, close, creat, dup, dup2, fnmatch, getopt, tzset,
  307.    tzsetwall, gettimeofday, ftime, localtime, gmtime, asctime, ctime, mktime,
  308.    endgrent, endpwent, getgrent, setgid, setegid, setgroupent, setgrent,
  309.    setuid, seteuid, opendir, closedir, readdir, telldir, seekdir, fchmod,
  310.    fcntl, fstat, ftruncate, getenv, setenv, gethostname, getpid, getwd, getgid,
  311.    getegid, getgrgid, getgrnam, hypot, index, ioctl, isatty, kill, link, lseek,
  312.    lstat, mkdir, mkfifo, mknod, mktemp, open, perror, pipe, socketpair,
  313.    getpwuid, getpwnam, getlogin, popen, pclose, read, readlink, regerror,
  314.    regcomp, regexec, regsub, remove, rename, rindex, rmdir, select, setjmp,
  315.    longjmp, signal, sigpause, sigprocmask, sigsetmask, sleep, stat, strcasecmp,
  316.    strncasecmp, strerror, strftime, symlink, tempnam, time, truncate, getuid,
  317.    geteuid, umask, unlink, utime, wait, waitpid, wait3, wait4, write
  318.  
  319.    These functions aim to be BSD compatible, however I based myself on
  320.    descriptions in the SunOS 4 man pages. Their behaviour is sometimes strange
  321.    or incomplete.
  322.  
  323.    Here is a summary of the differences with the standard Unix functions:
  324.  
  325.    alloca - This is Stefan Proels's alloca for SAS/C. It is implemented taking
  326.         advantage of the compiler's PROFILEing facility. If you compile
  327.         with the PROFILE switch turned on the compiler calls _PROLOG()
  328.         whenever a function is entered and _EPILOG() when the function
  329.         returns respectively. Refer to your SAS/C manuals for details.
  330.         This alloca() implementation uses those hooks to track alloca()ted
  331.         memory. Whenever a function returns all memory alloca()ted by that
  332.         function will be freed. Remember to compile with the PROFILE switch
  333.         the modules using alloca() if you want a working alloca(). If you
  334.         include alloca.h (which you have to do if you want its prototype)
  335.         you will be remembered of this ...
  336.  
  337.    gettimeofday - The dst field of the timezone is a (not very good) guess. It
  338.         shouldn't be used anyway.
  339.  
  340.    chown, fchown - These are do-nothing routines.
  341.  
  342.    opendir, closedir, ... - These interact with stat to avoid having to read
  343.         each directory entry twice when doing a readdir + stat loop.
  344.  
  345.    fchmod - The protection is only set when the file is closed. See also the
  346.         discussion of protection modes above.
  347.  
  348.    fcntl  - Only the F_GETFL & F_SETFL operations are supported.
  349.  
  350.    truncate, ftruncate - These will not necessarily work with all filing
  351.         systems. See the discussion of the AmigaDOS SetFileSize function.
  352.  
  353.    getenv - This checks local environment variables and then global ones. It
  354.         allocates memory for the variables value each time. This memory is
  355.         never freed (till the program exits).
  356.  
  357.    gethostname - This is obtained from the HOSTNAME environment variable at
  358.         startup.
  359.  
  360.    getuid, geteuid, getgid, getegid, getpwuid, getpwnam, getgrgid, getgrnam,
  361.    getlogin - These function only know about one user. He/She is:
  362.  
  363.     name: $USER (default "user")
  364.     home directory: $HOME (default "s:")
  365.     shell: $SHELL (default "bin:sh")
  366.     uid: 1, gid: 0
  367.  
  368.    ioctl  - Only FIONBIO & TIOCGWINSZ are known (for sockets all those
  369.             recognized by AmiTCP), plus a number of amiga specific ioctls
  370.         (for internal use). These are documented in include/amiga/ioctl.h.
  371.  
  372.    lseek  - Seeks beyond the end of a file are not implemented.
  373.  
  374.    mkfifo, mknod - These are 'do-nothing' routines.
  375.  
  376.    select - This implementation cannot detect 'exceptional' conditions
  377.             with pipes & sktpairs. By default FD_SETSIZE is 64. If you need
  378.         a large value you must #define FD_SETSIZE before including
  379.         <sys/types.h> and, if you use sockets, also set the external
  380.         variable _fd_setsize to the same value, e.g.:
  381.         #define FD_SETSIZE 128
  382.         #include <sys/types.h>
  383.         long _fd_setsize = FD_SETSIZE; /* only needed for sockets */
  384.  
  385.    umask  - This is a 'do-nothing' routine.
  386.  
  387.    unlink - You can unlink a file that your program has opened before you close
  388.         it. At that point, the AmigaDOS file is closed & deleted, though it
  389.         still appears open to read (it returns EOF), write, close, etc.
  390.         Also you can unlink a file even if it is protected from deletion
  391.         (the protection mode is changed first ...).
  392.  
  393.    utime  - Only the modification time can be changed (the others don't exist).
  394.  
  395.    wait3, wait4 - Resource usage is never returned.
  396.  
  397. Recompiling
  398. -----------
  399.  
  400. To recompile, simply run smake in the src directory. You can add DEF=USE_LOCAL
  401. to DEFS in smakefile if you want the system clock to store local time
  402. instead of GMT.
  403.  
  404.